package com.qmq.app.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmq.app.dao.IntroduceDao;
import com.qmq.app.entity.IntroduceEntity;
import com.qmq.app.service.QmqIntroduceService;
import com.qmq.common.exception.RRException;
import com.qmq.common.utils.Constant;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service("QmqIntroduceService")
public class QmqIntroduceServiceImpl extends ServiceImpl<IntroduceDao, IntroduceEntity> implements QmqIntroduceService {

    @Override
    public IntroduceEntity getByType(String type) {
        return this.getOne(Wrappers.<IntroduceEntity>lambdaQuery()
                .eq(IntroduceEntity::getType, type)
                .last(Constant.SEARCH_ONE));
    }

    @Override
    public void add(IntroduceEntity dto) {
        IntroduceEntity intro = this.getByType(dto.getType());
        if (Objects.nonNull(intro)) {
            throw new RRException("此类型已存在,请使用编辑");
        }
        this.save(dto);
    }

    @Override
    public void updateByDto(IntroduceEntity dto) {
        this.update(Wrappers.<IntroduceEntity>lambdaUpdate()
                .set(IntroduceEntity::getType, dto.getType())
                .set(IntroduceEntity::getContext, dto.getContext())
                .eq(IntroduceEntity::getId, dto.getId())
        );
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }
}
